<?php

/**
 * This is the model class for table "{{ptbhadarate}}".
 *
 * The followings are the available columns in table '{{ptbhadarate}}':
 * @property string $idptbhadarate
 * @property string $aresidential
 * @property string $acommercial
 * @property string $bresidential
 * @property string $bcommercial
 * @property string $cresidential
 * @property string $ccommercial
 * @property string $dresidential
 * @property string $dcommercial
 * @property string $idccfyear
 * @property string $idccward
 *
 * The followings are the available model relations:
 * @property Ccfyear $idccfyear0
 * @property Ccward $idccward0
 */
class Ptbhadarate extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Ptbhadarate the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{ptbhadarate}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('aresidential, acommercial, bresidential, bcommercial, cresidential, ccommercial, dresidential, dcommercial', 'length', 'max' => 15),
            array('idccfyear, idccward', 'length', 'max' => 10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('idcczone, idptbhadarate, aresidential, acommercial, bresidential, bcommercial, cresidential, ccommercial, dresidential, dcommercial, idccfyear, idccward', 'safe', 'on' => 'search'),
            array('idccfyear, idccward', 'validatefkeys'),
            array('aresidential, acommercial, bresidential, bcommercial, cresidential, ccommercial, dresidential, dcommercial', 'type', 'type' => 'float'),
        );
    }

    public function validatefkeys($attribute, $params) {
        $aux = $attribute . '0';
        if (!$this->$aux)
            $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}' => Yii::t('application', $attribute))));
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'idccfyear0' => array(self::BELONGS_TO, 'Ccfyear', 'idccfyear'),
            'idccward0' => array(self::BELONGS_TO, 'Ccward', 'idccward'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'idptbhadarate' => Yii::t('application', 'Idptbhadarate'),
            'aresidential' => Yii::t('application', 'Aresidential'),
            'acommercial' => Yii::t('application', 'Acommercial'),
            'bresidential' => Yii::t('application', 'Bresidential'),
            'bcommercial' => Yii::t('application', 'Bcommercial'),
            'cresidential' => Yii::t('application', 'Cresidential'),
            'ccommercial' => Yii::t('application', 'Ccommercial'),
            'dresidential' => Yii::t('application', 'Dresidential'),
            'dcommercial' => Yii::t('application', 'Dcommercial'),
            'idccfyear' => Yii::t('application', 'Idccfyear'),
            'idccward' => Yii::t('application', 'Idccward'),
        );
    }

    public $idcczone;

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        if (isset(Yii::app()->session['ccfyear'])) {
            $this->idccfyear = Yii::app()->session['ccfyear']->idccfyear;
        }

        $criteria = new CDbCriteria;
        $criteria->with = array('idccward0');
        $criteria->compare('idccward0.idcczone', $this->idcczone, true);

        $criteria->compare('idptbhadarate', $this->idptbhadarate, true);
        $criteria->compare('aresidential', $this->aresidential, true);
        $criteria->compare('acommercial', $this->acommercial, true);
        $criteria->compare('bresidential', $this->bresidential, true);
        $criteria->compare('bcommercial', $this->bcommercial, true);
        $criteria->compare('cresidential', $this->cresidential, true);
        $criteria->compare('ccommercial', $this->ccommercial, true);
        $criteria->compare('dresidential', $this->dresidential, true);
        $criteria->compare('dcommercial', $this->dcommercial, true);
        $criteria->compare('idccfyear', $this->idccfyear, true);
        $criteria->compare('idccward', $this->idccward, true);
//        echo $this->idcczone . 'kkk'; exit(0);

        $dataProvider = new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'idcczone',
                        'attributes' => array(
                            'idcczone' => array(
                                'asc' => 'idccward0.idcczone',
                                'desc' => 'idccward0.idcczone DESC',
                            ),
                            '*',
                        ),
                    ),
                ));

        if (isset($_POST['Ptbhadarate'])) {
            $ptbhadarates = $_POST['Ptbhadarate'];
            $models = array();
            foreach ($ptbhadarates as $key => $value) {
                $model = PtbhadarateController::loadModel($key);
                $model->attributes = $value;
                $models[] = $model;
            }
            $dataProvider->setData($models);
        }

        return $dataProvider;
    }

}